package org.jeecg.modules.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.constant.CommonCuckoo;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.cuckoo.enums.TransactionStatusEnum;
import org.jeecg.cuckoo.enums.TransactionTypeEnum;
import org.jeecg.modules.entity.CuCommissionManagement;
import org.jeecg.modules.entity.CuUserDetails;
import org.jeecg.modules.mapper.CuUserDetailsMapper;
import org.jeecg.modules.service.ICuCommissionManagementService;
import org.jeecg.modules.service.ICuUserDetailsService;
import org.jeecg.modules.service.ICuWalletaMnagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description: 用户明显表
 * @Author: jeecg-boot
 * @Date: 2024-07-11
 * @Version: V1.0
 */
@Service
public class CuUserDetailsServiceImpl extends ServiceImpl<CuUserDetailsMapper, CuUserDetails> implements ICuUserDetailsService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private CuUserDetailsMapper cuUserDetailsMapper;


    @Override
    public boolean updateById(CuUserDetails entity) {
        String key = CommonCuckoo.USER_INFO_CACHE + entity.getId() + CommonCuckoo.VIP_INFO_KEY;
        redisUtil.del(key);
        return super.updateById(entity);
    }

    @Override
    public boolean save(CuUserDetails cuUserDetails) {
        return super.save(cuUserDetails);
    }

    @Override
    public boolean removeById(String id) {
        String key = CommonCuckoo.USER_INFO_CACHE + id + CommonCuckoo.VIP_INFO_KEY;
        redisUtil.del(key);
        return super.removeById(id);
    }

    @Override
    public boolean removeByIds(List<String> ids) {
        for (String id : ids) {
            String key = CommonCuckoo.USER_INFO_CACHE + id + CommonCuckoo.VIP_INFO_KEY;
            redisUtil.del(key);
        }
        return super.removeByIds(ids);
    }

    @Override
    public String generateInvitationCode() {
        String code;
        do {
            code = RandomUtil.randomNumbers(6);
        } while (!isCodeExist(code));
        return code;
    }

    @Override
    public CuUserDetails getById(String id) {
        String key = CommonCuckoo.USER_INFO_CACHE + id + CommonCuckoo.VIP_INFO_KEY;
        // 先查询缓存是否存在
        // 存在直接返回
        CuUserDetails userDetails = (CuUserDetails) redisUtil.get(key);
        if (ObjectUtil.isNotEmpty(userDetails)) {
            return userDetails;
        }
        // 不存在查询结果放入缓存并返回
        userDetails = super.getById(id);
        redisUtil.set(key, userDetails, JwtUtil.EXPIRE_TIME * 2 / 1000);

        return userDetails;
    }

    /**
     * 检查邀请码是否已存在于数据库中
     *
     * @param code
     * @return
     */
    private boolean isCodeExist(String code) {
        CuUserDetails details = new CuUserDetails();
        details.setInvitCode(code);
        QueryWrapper<CuUserDetails> queryWrapper = new QueryWrapper<>(details);
        CuUserDetails userDetails = super.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(userDetails)) {
            return true;
        }
        return false;
    }

    @Override
    public CuUserDetails getByUsername(String userName) {
        if (StringUtils.isEmpty(userName)) {
            return null;
        }
        CuUserDetails details = new CuUserDetails();
        details.setUserName(userName);
        QueryWrapper<CuUserDetails> queryWrapper = new QueryWrapper<>(details);
        CuUserDetails userDetails = super.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(userDetails)) {
            return null;
        }
        return userDetails;
    }

    @Override
    public List<CuUserDetails> getParentList(String registerCode, Integer level) {
        return cuUserDetailsMapper.getParentList(registerCode, level);
    }
}
